package com.zimin.cloud.mine.web.spring;

import cn.hutool.extra.spring.SpringUtil;
import com.alibaba.fastjson.JSON;
import com.zimin.cloud.mine.pojo.system.business.OperationLogBO;
import com.zimin.cloud.system.api.IOperationLogFeign;
import com.zimin.common.constant.ServletConstant;
import com.zimin.common.enums.Number;
import com.zimin.common.utils.ServletUtil;
import com.zimin.shared.annotation.OpLog;
import lombok.extern.slf4j.Slf4j;
import org.aopalliance.intercept.MethodInterceptor;
import org.aopalliance.intercept.MethodInvocation;
import org.springframework.util.ObjectUtils;

/**
 * @author zimin
 * @since 2021/9/28 3:37 下午
 */
//@Component
@Slf4j
@Deprecated
public class OpLogInterceptor implements MethodInterceptor {

    private final IOperationLogFeign operationLogFeign;

    public OpLogInterceptor() {
        this.operationLogFeign = SpringUtil.getBean(IOperationLogFeign.class);
    }

    @Override
    public Object invoke(MethodInvocation methodInvocation) throws Throwable {
        var method = methodInvocation.getMethod();
        var opLog = method.getAnnotation(OpLog.class);
        log.info("========开始执行{}.{}========", methodInvocation.getThis().getClass(), method.getName());
        var start = System.currentTimeMillis();
        var result = methodInvocation.proceed();
        var end = System.currentTimeMillis();
        var runTime = end - start;
        log.info("========执行{}.{}结束,共耗时{}毫秒========", methodInvocation.getThis().getClass(), method.getName(), runTime);
        if (ObjectUtils.isEmpty(opLog)) {
            return result;
        }
        try {
            var request = ServletUtil.getRequest();
            var mouldName = opLog.module().name();
            var action = opLog.action();
            var loginUserName = ServletUtil.getUserName();
            var accountId = ServletUtil.getUserId();
            var params = opLog.saveParams() ? JSON.toJSONString(methodInvocation.getArguments()) : null;
            if (!ObjectUtils.isEmpty(request)) {
                var url = request.getRequestURL().toString();
                var requestMethod = request.getMethod();
                var ip = ServletUtil.getIp();
                log.info("========保存{}.{}日志========", methodInvocation.getThis().getClass(), method.getName());
                var operationLog = OperationLogBO.builder()
                        .businessType(getBusinessType(requestMethod)).title(action)
                        .method(method.getName()).requestMethod(requestMethod).operatorUrl(url)
                        .operatorIp(ip).operatorType(1).runTime(runTime)
                        .operatorParam(params).operatorName(loginUserName).account(accountId)
                        .build();
                operationLogFeign.saveOperationLog(operationLog);
                log.info("accountId:{},moudleName:{},action:{},loginUserName:{},params:{},url:{},method:{},ipAddr:{}",
                        accountId, mouldName, action, loginUserName, params, url, method, ip);
            }
        } catch (Exception e) {
            log.info("保存日志失败");
            e.printStackTrace();
        }
        return result;
    }


    /**
     * <p>获取业务类型</p>
     *
     * @param method
     * @return java.lang.Integer
     * @since 2022-04-11 11:25:54
     */
    private Integer getBusinessType(String method) {
        switch (method) {
            case ServletConstant.GET:
                return Number.ONE.getIntNum();
            case ServletConstant.POST:
                return Number.TWO.getIntNum();
            case ServletConstant.PUT:
                return Number.THREE.getIntNum();
            case ServletConstant.DELETE:
                return Number.FOUR.getIntNum();
            default:
                return Number.ZERO.getIntNum();
        }
    }
}
